/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author Gerben Feenstra
 */
@NamedQueries(value = {
    @NamedQuery(name = FieldPlan.Q_FIND_ALL_FIELD_PLANS, query = "select fp from FieldPlan fp")
})
@Entity
@Table(name = "FIELD_PLAN")
public class FieldPlan extends Bean implements Serializable, Persistable{
    public static final String Q_FIND_ALL_FIELD_PLANS = "findAllFieldPlans";
    private static final Logger LOGGER = Logger.getLogger(FieldPlan.class.getName());
    

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private long id;
        
    @Column(name = "PLAN_YEAR")
    private int planYear;
    
    @OneToMany(cascade=CascadeType.ALL, mappedBy="fieldPlan")
    private List<ProductionUnit> productionUnits;


    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public int getPlanYear() {
        return planYear;
    }

    public void setPlanYear(int planYear) {
        
        
        if(planYear!=this.planYear && this.planYear!=0) {
            LOGGER.warning("Attempt to change readonly property: Year of fieldplan. Property not changed.");
        }else{
            this.planYear = planYear;
        }
    }

    public List<ProductionUnit> getProductionUnits() {
        if(productionUnits == null){
            productionUnits = new ArrayList<ProductionUnit>();
        }
        return Collections.unmodifiableList(productionUnits);
    }
    
    public void addProductionUnit(ProductionUnit productionUnit){
        if(productionUnits == null){
            productionUnits = new ArrayList<ProductionUnit>();
        }
        productionUnits.add(productionUnit);
        getPropertyChangeSupport().firePropertyChange("productionUnit", null, productionUnit);
    }
    
    public void removeProductionUnit(ProductionUnit productionUnit){
        if(productionUnits == null){
            productionUnits = new ArrayList<ProductionUnit>();
        }
        productionUnits.remove(productionUnit);
        getPropertyChangeSupport().firePropertyChange("productionUnit", productionUnit, null);
    }
    
    @Override
    public String toString(){
        return this.getName();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        if(!super.equals(obj)){
           return false; 
        }
        final FieldPlan other = (FieldPlan) obj;
        if (this.id != other.id) {
            return false;
        }
        if (this.planYear != other.planYear) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = super.hashCode();
        hash = 31 * hash + (int) (this.id ^ (this.id >>> 32));
        hash = 31 * hash + this.planYear;
        return hash;
    }

    /**
     * Task builder class
     *
     * usage: Task task = new Task.Builder().setName("someName").build();
     *
     */
    public static class Builder {

        private FieldPlan fieldPlan;

        public Builder() {
            fieldPlan = new FieldPlan();
        }

        public Builder setPlanYear(int planYear) {
            fieldPlan.setPlanYear(planYear);
            return this;
        }

        public FieldPlan build() {
            return fieldPlan;
        }
    }
    
    
}
